﻿using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.Controls
{
    public partial class ZoneList : UserControl
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.ZoneSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public ZoneList()
        {
            InitializeComponent();
            gridView.DoubleClick += GridViewDoubleClick;
            editToolStripMenuItem.Click += EditToolStripMenuItemClick;
            addToolStripMenuItem.Click += AddToolStripMenuItemClick;
            changeSituationToolStripMenuItem.Click += ChangeSituationToolStripMenuItemClick;
            contextMenuStrip.Opening += ContextMenuStripOpening;
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            gridControl.DataSource = _controller.Zones;
            operatorRepositoryItemLookUpEdit.DataSource = _controller.Operators;            
        }

        private static Zonas GetClone(Zonas zone)
        {
            var document = new Zonas
                               {
                                   IdZona = zone.IdZona,
                                   Nome = zone.Nome,
                                   IdOperador = zone.IdOperador,
                                   Activo = zone.Activo
                               };
            return document;
        }

        private static void Merge(Zonas zone, Zonas document)
        {
            zone.Nome = document.Nome;
            zone.IdOperador = document.IdOperador;
            zone.Activo = document.Activo;
        }

        private void ShowZone(Zonas zone)
        {
            Zonas document = GetClone(zone);
            var window = new Zone {Controller = _controller, Document = document};
            if (DialogResult.OK != window.ShowDialog())
                return;
            Merge(zone, window.Document);
            _controller.SaveZone(zone);
        }

        #endregion

        void GridViewDoubleClick(object sender, System.EventArgs e)
        {
            var zone = (Zonas)gridView.GetRow(gridView.FocusedRowHandle);
            if (zone == null)
                return;
            ShowZone(zone);            
        }

        void EditToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var zone = (Zonas)gridView.GetRow(gridView.FocusedRowHandle);
            ShowZone(zone);
        }

        void AddToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var zone = new Zonas {IdZona = -1, Nome = string.Empty, Activo = true};
            ShowZone(zone);
        }

        void ChangeSituationToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var zone = (Zonas)gridView.GetRow(gridView.FocusedRowHandle);
            if (zone == null)
                return;
            zone.Activo = !zone.Activo;
            _controller.SaveZone(zone);
        }

        void ContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var entityType = (Zonas)gridView.GetRow(gridView.FocusedRowHandle);
            editToolStripMenuItem.Enabled = entityType != null;
            changeSituationToolStripMenuItem.Enabled = entityType != null;
            removeToolStripMenuItem.Enabled = false;
        } 

    }
}
